package com.TIEmulator;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory.Options;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;
import com.TIEmulator.JNI.CannotBindNLibInterfaceException;
import com.TIEmulator.JNI.NLib;
import com.TIEmulator.JNI.NativeEvents;
import java.io.PrintStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;
import javax.microedition.khronos.opengles.GL11Ext;

class EmulatorRenderer
  implements GLSurfaceView.Renderer, NativeEvents
{
  private static int TEX_BUF_HEIGHT;
  private static int TEX_BUF_WIDTH;
  private static int mViewHeight;
  private static int mViewWidth;
  private static BitmapFactory.Options sBitmapOptions = new BitmapFactory.Options();
  private ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * TEX_BUF_HEIGHT * TEX_BUF_WIDTH);
  private boolean emulation_running = false;
  private Context mContext = null;
  private int[] mCropWorkspace = new int[4];
  private GLSurfaceView mGLView;
  protected int mTex = -1;
  private int[] mTextureNameWorkspace = new int[1];
  protected int[] mtexBuf = new int[1];
  private boolean startupDialogDismiss = false;

  static
  {
    TEX_BUF_WIDTH = 256;
    TEX_BUF_HEIGHT = 128;
  }

  public EmulatorRenderer(Context paramContext, GLSurfaceView paramGLSurfaceView)
  {
    this.byteBuffer.order(ByteOrder.BIG_ENDIAN);
    sBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
    this.mGLView = paramGLSurfaceView;
    this.mContext = paramContext;
    try
    {
      NLib.setListener(this);
      if (NLib.LoadLib())
        break label121;
      callFinishOnce();
      label106: label121: return;
    }
    catch (CannotBindNLibInterfaceException localCannotBindNLibInterfaceException)
    {
      localCannotBindNLibInterfaceException.printStackTrace();
      callFinishOnce();
      break label106:
      Log.v("EmulatorRenderer", " init successfull!");
    }
  }

  private void callFinishOnce()
  {
    if (this.mContext == null)
      return;
    ((Activity)this.mContext).finish();
    this.mContext = null;
  }

  public void OnFatalError(String paramString)
  {
    Log.d(super.toString(), "FATAL ERROR CALLBACK raised: " + paramString + " ===> Activity calls finish()");
  }

  public void OnImageUpdate()
  {
    if (this.emulation_running)
      if (this.startupDialogDismiss)
        if (!(((RenderCb)this.mContext).dismissStartupDialog()))
          break label44;
    for (int i = 0; ; i = 1)
    {
      this.startupDialogDismiss = i;
      this.mGLView.requestRender();
      label44: return;
    }
  }

  public void OnPostMessage(String paramString)
  {
    if (!(this.mContext instanceof RenderCb))
      return;
    ((RenderCb)this.mContext).updateStartupDialog(paramString);
  }

  public void OnTIEmuStopped()
  {
    System.out.println("OnTIEmuStopped callback called! ");
    callFinishOnce();
  }

  protected void finalize()
    throws Throwable
  {
    if (this.emulation_running)
      NLib.stop();
    super.finalize();
  }

  protected int loadBB(GL10 paramGL10)
  {
    int i = -1;
    if ((this.mContext != null) && (paramGL10 != null))
    {
      paramGL10.glGenTextures(1, this.mTextureNameWorkspace, 0);
      i = this.mTextureNameWorkspace[0];
      paramGL10.glBindTexture(3553, i);
      paramGL10.glTexParameterf(3553, 10241, 9728.0F);
      paramGL10.glTexParameterf(3553, 10240, 9728.0F);
      paramGL10.glTexParameterf(3553, 10242, 33071.0F);
      paramGL10.glTexParameterf(3553, 10243, 33071.0F);
      paramGL10.glTexEnvf(8960, 8704, 7681.0F);
      this.mCropWorkspace[0] = 0;
      this.mCropWorkspace[1] = 0;
      this.mCropWorkspace[2] = NLib.getWidth();
      this.mCropWorkspace[3] = NLib.getHeight();
      this.byteBuffer.order(ByteOrder.BIG_ENDIAN);
      int j = paramGL10.glGetError();
      if (j != 0)
        Log.e("SpriteMethodTest", "Texture Load GLError: " + j);
    }
    return i;
  }

  public void onDrawFrame(GL10 paramGL10)
  {
    paramGL10.glActiveTexture(this.mTex);
    paramGL10.glClientActiveTexture(this.mTex);
    this.byteBuffer.position(0);
    paramGL10.glBindTexture(3553, this.mTex);
    ((GL11)paramGL10).glTexParameteriv(3553, 35741, this.mCropWorkspace, 0);
    paramGL10.glTexImage2D(3553, 0, 6408, TEX_BUF_WIDTH, TEX_BUF_HEIGHT, 0, 6408, 5121, this.byteBuffer);
    ((GL11Ext)paramGL10).glDrawTexiOES(0, 0, 0, mViewWidth, mViewHeight);
    paramGL10.glEnable(2929);
    paramGL10.glDepthFunc(515);
  }

  public void onPause()
  {
    this.mGLView.onPause();
    this.emulation_running = false;
    this.startupDialogDismiss = false;
    NLib.stop();
  }

  public void onResume()
  {
    this.mGLView.onResume();
    this.startupDialogDismiss = true;
    if (!(NLib.startEmulation(this.byteBuffer)))
      callFinishOnce();
    while (true)
    {
      return;
      this.emulation_running = true;
    }
  }

  public void onSurfaceChanged(GL10 paramGL10, int paramInt1, int paramInt2)
  {
    mViewWidth = paramInt1;
    mViewHeight = paramInt2;
    Log.v(super.toString(), "onSurfaceChanged: ==> New View Size: [" + paramInt1 + "," + paramInt2 + "]");
  }

  public void onSurfaceCreated(GL10 paramGL10, EGLConfig paramEGLConfig)
  {
    paramGL10.glHint(3152, 4353);
    paramGL10.glClearColor(0.5F, 0.5F, 0.5F, 1.0F);
    paramGL10.glShadeModel(7424);
    paramGL10.glDisable(2929);
    paramGL10.glEnable(3553);
    paramGL10.glDisable(3024);
    paramGL10.glDisable(2896);
    paramGL10.glDisable(3042);
    paramGL10.glDisable(2929);
    paramGL10.glHint(3154, 4354);
    paramGL10.glClear(16640);
    this.mTex = loadBB(paramGL10);
  }

  public void sizeChanged(GL10 paramGL10, int paramInt1, int paramInt2)
  {
    Log.v(super.toString(), "sizeChanged: ==> new Viewport: [" + paramInt1 + "," + paramInt2 + "]");
    paramGL10.glViewport(0, 0, paramInt1, paramInt2);
    paramGL10.glMatrixMode(5889);
    paramGL10.glLoadIdentity();
    paramGL10.glOrthof(0.0F, paramInt1, 0.0F, 0.0F, paramInt2, 1.0F);
    paramGL10.glBlendFunc(770, 771);
    paramGL10.glColor4x(65536, 65536, 65536, 65536);
    paramGL10.glEnable(3553);
    paramGL10.glMatrixMode(5888);
  }

  public static abstract interface RenderCb
  {
    public abstract boolean dismissStartupDialog();

    public abstract void updateStartupDialog(String paramString);
  }
}
